package com.yzy.fly.util.asm;

import org.apache.commons.io.FileUtils;
import org.objectweb.asm.*;

import java.io.File;

public class Test21 implements Opcodes {

    public static void main(String[] args) throws Exception {
        String relative_path = "sample/HelloWorld.class";
//        String filepath = "D:\\gitee\\vhlSpringCloud\\fly\\target\\classes\\com\\yzy\\bi\\entity\\dealResult\\Test2.class";
        String filepath = "D:\\gitee\\fly\\target\\classes\\com\\yzy\\bi\\util\\asm\\TestEnum.class";
//        String relative_path = "D:\\gitee\\vhlSpringCloud\\bi\\target\\classes\\com\\yzy\\bi\\entity\\core\\Test.class";
//        String filepath = FileUtils.getFilePath(relative_path);

        // (1) 生成byte[]内容
        byte[] bytes = dump();
        File classFile = new File(filepath);
        if (classFile.exists()) {
            classFile.delete();

        }
        classFile.createNewFile();
        // (2) 保存byte[]到文件
        FileUtils.writeByteArrayToFile(classFile, bytes);
    }
        public static byte[] dump () throws Exception {


                    ClassWriter classWriter = new ClassWriter(0);
                    FieldVisitor fieldVisitor;
                    RecordComponentVisitor recordComponentVisitor;
                    MethodVisitor methodVisitor;
                    AnnotationVisitor annotationVisitor0;

                    classWriter.visit(V1_8, ACC_PUBLIC | ACC_FINAL | ACC_SUPER | ACC_ENUM, "com/yzy/fly/type/FileType", "Ljava/lang/Enum<Lcom/yzy/fly/type/FileType;>;", "java/lang/Enum", null);

                    {
                        fieldVisitor = classWriter.visitField(ACC_PUBLIC | ACC_FINAL | ACC_STATIC | ACC_ENUM, "PNG", "Lcom/yzy/fly/type/FileType;", null, null);
                        fieldVisitor.visitEnd();
                    }
                    {
                        fieldVisitor = classWriter.visitField(ACC_PUBLIC | ACC_FINAL | ACC_STATIC | ACC_ENUM, "JPG", "Lcom/yzy/fly/type/FileType;", null, null);
                        fieldVisitor.visitEnd();
                    }
                    {
                        fieldVisitor = classWriter.visitField(ACC_PUBLIC | ACC_FINAL | ACC_STATIC | ACC_ENUM, "JAVA", "Lcom/yzy/fly/type/FileType;", null, null);
                        fieldVisitor.visitEnd();
                    }
                    {
                        fieldVisitor = classWriter.visitField(ACC_PUBLIC | ACC_FINAL | ACC_STATIC | ACC_ENUM, "CLASS", "Lcom/yzy/fly/type/FileType;", null, null);
                        fieldVisitor.visitEnd();
                    }
                    {
                        fieldVisitor = classWriter.visitField(ACC_PRIVATE, "type", "Ljava/lang/String;", null, null);
                        fieldVisitor.visitEnd();
                    }
                    {
                        fieldVisitor = classWriter.visitField(ACC_PRIVATE, "value", "Ljava/lang/String;", null, null);
                        fieldVisitor.visitEnd();
                    }
                    {
                        fieldVisitor = classWriter.visitField(ACC_PRIVATE | ACC_FINAL | ACC_STATIC | ACC_SYNTHETIC, "$VALUES", "[Lcom/yzy/fly/type/FileType;", null, null);
                        fieldVisitor.visitEnd();
                    }
                    {
                        methodVisitor = classWriter.visitMethod(ACC_PUBLIC | ACC_STATIC, "values", "()[Lcom/yzy/fly/type/FileType;", null, null);
                        methodVisitor.visitCode();
                        methodVisitor.visitFieldInsn(GETSTATIC, "com/yzy/fly/type/FileType", "$VALUES", "[Lcom/yzy/fly/type/FileType;");
                        methodVisitor.visitMethodInsn(INVOKEVIRTUAL, "[Lcom/yzy/fly/type/FileType;", "clone", "()Ljava/lang/Object;", false);
                        methodVisitor.visitTypeInsn(CHECKCAST, "[Lcom/yzy/fly/type/FileType;");
                        methodVisitor.visitInsn(ARETURN);
                        methodVisitor.visitMaxs(1, 0);
                        methodVisitor.visitEnd();
                    }
                    {
                        methodVisitor = classWriter.visitMethod(ACC_PUBLIC | ACC_STATIC, "valueOf", "(Ljava/lang/String;)Lcom/yzy/fly/type/FileType;", null, null);
                        methodVisitor.visitCode();
                        methodVisitor.visitLdcInsn(Type.getType("Lcom/yzy/fly/type/FileType;"));
                        methodVisitor.visitVarInsn(ALOAD, 0);
                        methodVisitor.visitMethodInsn(INVOKESTATIC, "java/lang/Enum", "valueOf", "(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/Enum;", false);
                        methodVisitor.visitTypeInsn(CHECKCAST, "com/yzy/fly/type/FileType");
                        methodVisitor.visitInsn(ARETURN);
                        methodVisitor.visitMaxs(2, 1);
                        methodVisitor.visitEnd();
                    }
                    {
                        methodVisitor = classWriter.visitMethod(ACC_PRIVATE, "<init>", "(Ljava/lang/String;ILjava/lang/String;Ljava/lang/String;)V", "(Ljava/lang/String;Ljava/lang/String;)V", null);
                        methodVisitor.visitCode();
                        methodVisitor.visitVarInsn(ALOAD, 0);
                        methodVisitor.visitVarInsn(ALOAD, 1);
                        methodVisitor.visitVarInsn(ILOAD, 2);
                        methodVisitor.visitMethodInsn(INVOKESPECIAL, "java/lang/Enum", "<init>", "(Ljava/lang/String;I)V", false);
                        methodVisitor.visitVarInsn(ALOAD, 0);
                        methodVisitor.visitVarInsn(ALOAD, 3);
                        methodVisitor.visitFieldInsn(PUTFIELD, "com/yzy/fly/type/FileType", "type", "Ljava/lang/String;");
                        methodVisitor.visitVarInsn(ALOAD, 0);
                        methodVisitor.visitVarInsn(ALOAD, 4);
                        methodVisitor.visitFieldInsn(PUTFIELD, "com/yzy/fly/type/FileType", "value", "Ljava/lang/String;");
                        methodVisitor.visitInsn(RETURN);
                        methodVisitor.visitMaxs(3, 5);
                        methodVisitor.visitEnd();
                    }
                    {
                        methodVisitor = classWriter.visitMethod(ACC_PUBLIC, "setType", "(Ljava/lang/String;)V", null, null);
                        methodVisitor.visitCode();
                        methodVisitor.visitVarInsn(ALOAD, 0);
                        methodVisitor.visitVarInsn(ALOAD, 1);
                        methodVisitor.visitFieldInsn(PUTFIELD, "com/yzy/fly/type/FileType", "type", "Ljava/lang/String;");
                        methodVisitor.visitInsn(RETURN);
                        methodVisitor.visitMaxs(2, 2);
                        methodVisitor.visitEnd();
                    }
                    {
                        methodVisitor = classWriter.visitMethod(ACC_PUBLIC, "getType", "()Ljava/lang/String;", null, null);
                        methodVisitor.visitCode();
                        methodVisitor.visitVarInsn(ALOAD, 0);
                        methodVisitor.visitFieldInsn(GETFIELD, "com/yzy/fly/type/FileType", "type", "Ljava/lang/String;");
                        methodVisitor.visitInsn(ARETURN);
                        methodVisitor.visitMaxs(1, 1);
                        methodVisitor.visitEnd();
                    }
                    {
                        methodVisitor = classWriter.visitMethod(ACC_PUBLIC, "setValue", "(Ljava/lang/String;)V", null, null);
                        methodVisitor.visitCode();
                        methodVisitor.visitVarInsn(ALOAD, 0);
                        methodVisitor.visitVarInsn(ALOAD, 1);
                        methodVisitor.visitFieldInsn(PUTFIELD, "com/yzy/fly/type/FileType", "value", "Ljava/lang/String;");
                        methodVisitor.visitInsn(RETURN);
                        methodVisitor.visitMaxs(2, 2);
                        methodVisitor.visitEnd();
                    }
                    {
                        methodVisitor = classWriter.visitMethod(ACC_PUBLIC, "getValue", "()Ljava/lang/String;", null, null);
                        methodVisitor.visitCode();
                        methodVisitor.visitVarInsn(ALOAD, 0);
                        methodVisitor.visitFieldInsn(GETFIELD, "com/yzy/fly/type/FileType", "value", "Ljava/lang/String;");
                        methodVisitor.visitInsn(ARETURN);
                        methodVisitor.visitMaxs(1, 1);
                        methodVisitor.visitEnd();
                    }
                    {
                        methodVisitor = classWriter.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
                        methodVisitor.visitCode();
                        methodVisitor.visitTypeInsn(NEW, "com/yzy/fly/type/FileType");
                        methodVisitor.visitInsn(DUP);
                        methodVisitor.visitLdcInsn("PNG");
                        methodVisitor.visitInsn(ICONST_0);
                        methodVisitor.visitLdcInsn("png");
                        methodVisitor.visitLdcInsn(".png");
                        methodVisitor.visitMethodInsn(INVOKESPECIAL, "com/yzy/fly/type/FileType", "<init>", "(Ljava/lang/String;ILjava/lang/String;Ljava/lang/String;)V", false);
                        methodVisitor.visitFieldInsn(PUTSTATIC, "com/yzy/fly/type/FileType", "PNG", "Lcom/yzy/fly/type/FileType;");
                        methodVisitor.visitTypeInsn(NEW, "com/yzy/fly/type/FileType");
                        methodVisitor.visitInsn(DUP);
                        methodVisitor.visitLdcInsn("JPG");
                        methodVisitor.visitInsn(ICONST_1);
                        methodVisitor.visitLdcInsn("jpg");
                        methodVisitor.visitLdcInsn(".jpg");
                        methodVisitor.visitMethodInsn(INVOKESPECIAL, "com/yzy/fly/type/FileType", "<init>", "(Ljava/lang/String;ILjava/lang/String;Ljava/lang/String;)V", false);
                        methodVisitor.visitFieldInsn(PUTSTATIC, "com/yzy/fly/type/FileType", "JPG", "Lcom/yzy/fly/type/FileType;");
                        methodVisitor.visitTypeInsn(NEW, "com/yzy/fly/type/FileType");
                        methodVisitor.visitInsn(DUP);
                        methodVisitor.visitLdcInsn("JAVA");
                        methodVisitor.visitInsn(ICONST_2);
                        methodVisitor.visitLdcInsn("java");
                        methodVisitor.visitLdcInsn(".java");
                        methodVisitor.visitMethodInsn(INVOKESPECIAL, "com/yzy/fly/type/FileType", "<init>", "(Ljava/lang/String;ILjava/lang/String;Ljava/lang/String;)V", false);
                        methodVisitor.visitFieldInsn(PUTSTATIC, "com/yzy/fly/type/FileType", "JAVA", "Lcom/yzy/fly/type/FileType;");
                        methodVisitor.visitTypeInsn(NEW, "com/yzy/fly/type/FileType");
                        methodVisitor.visitInsn(DUP);
                        methodVisitor.visitLdcInsn("CLASS");
                        methodVisitor.visitInsn(ICONST_3);
                        methodVisitor.visitLdcInsn("class");
                        methodVisitor.visitLdcInsn(".class");
                        methodVisitor.visitMethodInsn(INVOKESPECIAL, "com/yzy/fly/type/FileType", "<init>", "(Ljava/lang/String;ILjava/lang/String;Ljava/lang/String;)V", false);
                        methodVisitor.visitFieldInsn(PUTSTATIC, "com/yzy/fly/type/FileType", "CLASS", "Lcom/yzy/fly/type/FileType;");
                        methodVisitor.visitInsn(ICONST_4);
                        methodVisitor.visitTypeInsn(ANEWARRAY, "com/yzy/fly/type/FileType");
                        methodVisitor.visitInsn(DUP);
                        methodVisitor.visitInsn(ICONST_0);
                        methodVisitor.visitFieldInsn(GETSTATIC, "com/yzy/fly/type/FileType", "PNG", "Lcom/yzy/fly/type/FileType;");
                        methodVisitor.visitInsn(AASTORE);
                        methodVisitor.visitInsn(DUP);
                        methodVisitor.visitInsn(ICONST_1);
                        methodVisitor.visitFieldInsn(GETSTATIC, "com/yzy/fly/type/FileType", "JPG", "Lcom/yzy/fly/type/FileType;");
                        methodVisitor.visitInsn(AASTORE);
                        methodVisitor.visitInsn(DUP);
                        methodVisitor.visitInsn(ICONST_2);
                        methodVisitor.visitFieldInsn(GETSTATIC, "com/yzy/fly/type/FileType", "JAVA", "Lcom/yzy/fly/type/FileType;");
                        methodVisitor.visitInsn(AASTORE);
                        methodVisitor.visitInsn(DUP);
                        methodVisitor.visitInsn(ICONST_3);
                        methodVisitor.visitFieldInsn(GETSTATIC, "com/yzy/fly/type/FileType", "CLASS", "Lcom/yzy/fly/type/FileType;");
                        methodVisitor.visitInsn(AASTORE);
                        methodVisitor.visitFieldInsn(PUTSTATIC, "com/yzy/fly/type/FileType", "$VALUES", "[Lcom/yzy/fly/type/FileType;");
                        methodVisitor.visitInsn(RETURN);
                        methodVisitor.visitMaxs(6, 0);
                        methodVisitor.visitEnd();
                    }
                    classWriter.visitEnd();

                    return classWriter.toByteArray();
                }
            }

